# 存储收货单文件，更新相关订单状态为 已完成， 需求单状态为 已完成
import sys
sys.path.append("..")
import json
from flask import request
from create_id import get_id
from db_client import *

def store_receive_file():
    # 存储收货单文件
    #print(1)
    token = request.headers.get('token')
    flag, right = token_client.check_token(token)
    if not flag:
        t = {
            'state': 'false',
            'msg': '存储收货单文件失败'
        }
        return json.dumps(t, ensure_ascii=False)
    else:
        request_data = request.get_data()
        request_data = request_data.decode('utf-8')

        data = json.loads(request_data)
        #print(data)
        count = data['count']
        item_id = data['item_id']
        receive_date = data['receive_date']
        order_id = data['order_id']
        content = data['content']
        staff_id = data['receive_staff_id']

        _id = get_id('SW')

        if not staff_id:
            t = {
                'state': 'false',
                'msg': '员工工号错误'
            }
            return json.dumps(t, ensure_ascii=False)

        if count == '':
            t = {
                'state': 'false',
                'msg': '接受数量错误'
            }
            return json.dumps(t, ensure_ascii=False)

        # 存储收货单
        flag2 = mysql_client.store_receive_file(count, item_id, receive_date, order_id, _id, content, staff_id)
        if flag2:
            t = {
                'state': 'true',
                'msg': 'success'
            }

            # 更新相关订单状态，以及订单的总接受数量

            update_flag = mysql_client.update_order_receive_num(order_id, count)
            if not update_flag:
                t = {
                    'state': 'false',
                    'msg': '相关订单更新收货数量失败，稍后再试。'
                }
                return json.dumps(t, ensure_ascii=False)

            received_count = mysql_client.get_order_receive_count(order_id)
            order_count = mysql_client.get_order_count(order_id)
            received_count = int(received_count)
            order_count = int(order_count)
            data = {}
            #print(received_count, order_count)
            if received_count < order_count:
                data = {
                    'status': '部分收货'
                }
            else:
                data = {
                    'status': '已完成'
                }
            #print(data)
            mysql_client.update_order(data, order_id)

            # 更新相关需求单状态
            plan_id = mysql_client.search_plan_of_order(order_id)
            demand_ids = mysql_client.search_demands_of_plan(plan_id)
            demand_ids = demand_ids.split(',')
            for demand_id in demand_ids:
                data = {
                    'de_if': '已完成'
                }
                mysql_client.update_demand(data, demand_id)

            return json.dumps(t, ensure_ascii=False)
        else:
            t = {
                'state': 'false',
                'msg': '提交收货单文件失败，稍后再试。'
            }
            return json.dumps(t, ensure_ascii=False)


if __name__ == '__main__':
    store_receive_file()

